﻿using System;
using System.IO;

namespace AB.Common
{
    /// <summary>
    /// 路径帮助类
    /// </summary>
    public class PathHelper
    {
        /// <summary>
        /// 获取绝对路径
        /// </summary>
        /// <param name="absolutePath">绝对路径</param>
        /// <param name="relativePath">相对路径</param>
        /// <returns></returns>
        public static string MyCombine(string absolutePath, string relativePath)
        {
            if (Path.IsPathRooted(relativePath))
            {
                return relativePath;
            }
            if (!Path.IsPathRooted(relativePath) && Path.IsPathRooted(absolutePath))
            {
                string path = absolutePath;
                string[] arry = relativePath.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < arry.Length; i++)
                {
                    if (arry[i] == ".")
                    {

                    }
                    else if (arry[i] == "..")
                    {
                        int index = path.LastIndexOf(@"\");
                        while (index == (path.Length - 1))
                        {
                            path = path.Substring(0, path.Length - 1);
                            index = path.LastIndexOf(@"\");
                        }
                        path = path.Substring(0, index);
                    }
                    else
                    {
                        int index = path.LastIndexOf(@"\");
                        if (index == (path.Length - 1))
                        {
                            path += arry[i];
                        }
                        else
                        {
                            path += (@"\" + arry[i]);
                        }
                    }
                }
                return path;
            }
            else
            {
                return null;
            }
        }
    }
}
